﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using System.Transactions;

using Toenda.Foundation.Data;

namespace Toenda.Foundation.Messaging {
	/// <summary>
	/// Provides properties for a specific version of a <see cref="Message" />.
	/// </summary>
	public class SystemMessage : Message {
		#region "Data Object"

		/// <summary>
		/// Gets or sets the ticket id.
		/// </summary>
		/// <value>The ticket id.</value>
		public int? TicketId { get; set; }

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public int? UserId { get; set; }

		/// <summary>
		/// Gets or sets from user id.
		/// </summary>
		/// <value>From user id.</value>
		public int? FromUserId { get; set; }

		/// <summary>
		/// Gets or sets the last name of from user.
		/// </summary>
		/// <value>The last name of from user.</value>
		public string FromUserLastName { get; set; }

		/// <summary>
		/// Gets or sets the first name of from user.
		/// </summary>
		/// <value>The first name of from user.</value>
		public string FromUserFirstName { get; set; }

		/// <summary>
		/// Gets or sets from user partner id.
		/// </summary>
		/// <value>From user partner id.</value>
		public string FromUserPartnerId { get; set; }

		//[DataMember]
		//public User User { get; set; }

		//[DataMember]
		//public User FromUser { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="SystemMessage"/> class.
		/// </summary>
		/// <param name="record"></param>
		public SystemMessage(IDataRecord record) 
			: base(record) {
			if(record == null) {
				throw new ArgumentNullException("record");
			}

			this.TicketId = (record["ticketId"] == DBNull.Value ? null : (int?)record["ticketId"]);
			this.UserId = (record["userId"] == DBNull.Value ? null : (int?)record["userId"]);
			this.FromUserId = (record["fromUserId"] == DBNull.Value ? null : (int?)record["fromUserId"]);

			if(record["firstName"] != DBNull.Value
			|| record["lastName"] != DBNull.Value
			|| record["partnerId"] != DBNull.Value) {
				this.FromUserFirstName = (record["firstName"] == DBNull.Value ? null : record["firstName"].ToString());
				this.FromUserLastName = (record["lastName"] == DBNull.Value ? null : record["lastName"].ToString());
				this.FromUserPartnerId = (record["partnerId"] == DBNull.Value ? null : record["partnerId"].ToString());
			}
			else {
				this.FromUserFirstName = "System";
				this.FromUserLastName = "";
				this.FromUserPartnerId = "";
			}

			// load the user object
			//using(SecurityInfoClient sic = new SecurityInfoClient()) {
			//    if(this.UserId.HasValue) {
			//        this.User = sic.GetUser(this.UserId.Value);
			//    }

			//    if(this.FromUserId.HasValue) {
			//        this.FromUser = sic.GetUser(this.FromUserId.Value);
			//    }
			//}
		}

		#endregion
	}
}
